package com.rem.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rem.mp.mapper.UserMapper;
import com.rem.mp.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;

/**
 * mp 查询
 *
 * @Author Rem
 * @Date 2020-02-28
 */

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyApplicationTest2 {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id查询一个
     */
    @Test
    public void testselectById() {
        User user = userMapper.selectById(3L);
        System.err.println(user);
    }

    /**
     * 根据多个id批量查询
     */
    @Test
    public void testSelectOne() {
        List<Long> longs = Arrays.asList(4L, 5L);
        List<User> users = userMapper.selectBatchIds(longs);
        users.forEach(System.out::println);
    }

    /**
     * 根据条件查询一个
     * 没有数据返回null  多条数据报错
     */
    @Test
    public void testSelectBatchIds() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "张三");
        User user = userMapper.selectOne(wrapper);
        System.err.println(user);
    }

    /**
     * 查询记录数
     */
    @Test
    public void testSelectCount() {

        //所有记录数  wrapper 设为空即可
        //Integer integer = userMapper.selectCount(null);

        //查询年龄大于25岁的有多少人
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 25);
        Integer integer = userMapper.selectCount(wrapper);
        System.err.println(integer);
    }

    /**
     * 查询所有
     */
    @Test
    public void testSelect() {
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /**
     * 条件查询
     */
    @Test
    public void testSelect2() {
        //查询年龄大于20岁的
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 20);
        List<User> userList = userMapper.selectList(wrapper);
        userList.forEach(System.err::println);
    }

    /**
     * 分页查询
     * SELECT id, email AS mail, name, user_name, age FROM tb_user WHERE (age > ?) ORDER BY age ASC LIMIT ?,?
     */
    @Test
    public void testSelectPage() {
        //SELECT id, email AS mail, name, user_name, age FROM tb_user WHERE (age > ?) ORDER BY age ASC LIMIT ?,?
        //查询年龄大于20岁的第二页的数据
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 20).orderByAsc("age");

        //查询第二页数据  每页三条   current：0和1都是只第一页
        Page<User> page = new Page<>(2L, 3L);

        IPage<User> userIPage = userMapper.selectPage(page, wrapper);

        System.err.println("总记录:" + userIPage.getRecords().toString());
        System.err.println("总页数:" + userIPage.getPages());
        System.err.println("总条数:" + userIPage.getTotal());
        System.err.println("当前页:" + userIPage.getCurrent());
        System.err.println("页大小:" + userIPage.getSize());
    }

    /**
     * 分页查询  只要记录 不需要记录数
     */
    @Test
    public void testSelectPage2() {
        //查询年龄大于20岁的第二页的数据
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 20).orderByAsc("age");

        //查询第二页数据  每页三条  不查询总记录数
        //Page(long current, long size, boolean isSearchCount)
        Page<User> page = new Page<>(2L, 3L, false);

        IPage<User> userIPage = userMapper.selectPage(page, wrapper);

        System.err.println("总记录:" + userIPage.getRecords().toString());
        System.err.println("总页数:" + userIPage.getPages());
        System.err.println("总条数:" + userIPage.getTotal());
        System.err.println("当前页:" + userIPage.getCurrent());
        System.err.println("页大小:" + userIPage.getSize());
    }


    /**
     * 自定义mapper.xml的使用
     */
    @Test
    public void testSelectMapper() {
        User user = userMapper.getById(2L);
        System.out.println(user);
    }


    /**
     * 自定义sql的使用
     * SELECT * FROM tb_user WHERE age > ?
     */
    @Test
    public void selectMy() {
        //SELECT * FROM tb_user WHERE age > ?
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 30);
        List<User> userList = userMapper.selectAll(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /**
     * 自定义分页
     */

    @Test
    public void selectPageUser() {
        Page<User> page = new Page<>(1, 3);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 25);
        IPage<User> userIPage = userMapper.selectPageUser(page, wrapper);
        System.err.println("总记录:" + userIPage.getRecords().toString());
        System.err.println("总条数:" + userIPage.getTotal());
    }

}